import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import junit.framework.TestCase;


public class XPathTest extends TestCase {
	public void testXpathRead()
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true); // never forget this!
		DocumentBuilder builder;
		try {
			builder = factory.newDocumentBuilder();
			Document doc = builder.parse("books.xml");
			XPathFactory xf = XPathFactory.newInstance();
			XPath xpath = xf.newXPath();
			//XPathExpression expr = xpath.compile("//book[author='Neal Stephenson']/title/text()");
			XPathExpression expr = xpath.compile("node()");
			NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
			for (int i = 0; i < nodes.getLength(); i++) {
				// some XMLs contain illegal nodes which are carriage return chars \n\t they should be ignored
				if(nodes.item(i).getLocalName()!=null)
				{
					// print node names
					System.out.println(nodes.item(i).getLocalName());
					// print node values
		    		System.out.println(nodes.item(i).getNodeValue());
				}
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}	
	
	public void testHash(){
		HashMap<String,String> ha = new HashMap<String,String>();
		HashMap<String, String>  hash = new HashMap<String,String>();
		hash.put("A1", "Q1");
		hash.put("A2", "Q2");
		hash.put("A3", "Q3");
		hash.put("A4", "Q4");
		hash.put("A5", "Q5");
		for (Map.Entry<String,String> entry : hash.entrySet()) { 
		    String key = entry.getKey(); 
		    String value = entry.getValue(); 
		    //System.out.println(key);
		    //System.out.println(value);

		} 

	}
	public void testTree(){
		TreeMap<String,String> hash = new TreeMap<String,String>();
		hash.put("A1", "Q1");
		hash.put("A2", "Q2");
		hash.put("A3", "Q3");
		hash.put("A4", "Q4");
		hash.put("A5", "Q5");
		/*for (Map.Entry<String,String> entry : hash.entrySet()) {*/
		Iterator<Entry<String,String>> i = hash.entrySet().iterator();
		int j = 0;
		while(i.hasNext() && j <3){
			Entry<String,String> entry = i.next();
		    String key = entry.getKey(); 
		    String value = entry.getValue(); 
		    System.out.println(key);
		    System.out.println(value);
		    j++;
		}

	} 
	public void testString(){
		String name = "hello/world/bye/humanity";
		System.out.println(name.substring(0,name.lastIndexOf("/")));
	}
	public void testWeirdHash(){
		HashMap<String,Integer> ha = new HashMap<String,Integer>();
		ha.put("title", new Integer(1));
		ha.put("author", new Integer(2));
		ha.put("publisher", new Integer(3));
		ha.put("price", new Integer(4));
		ha.put("chapter", new Integer(5));
		System.out.println(ha.get("title"));
	}
}